<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search">
      <el-form ref="search" :inline="true" :model="search">
        <!-- 商品类型搜索 -->
        <el-form-item label="商品类型搜索:" size="medium">
          <el-input v-model="search.className" clearable placeholder="请输入" />
        </el-form-item>
        <el-button
          type="primary"
          class="my-primary1"
          icon="el-icon-search"
          @click="getGoodsType"
        >查询</el-button>
      </el-form>
    </div>

    <!-- 表单区域 -->
    <div class="result">
      <!-- 头部 -->
      <el-row class="operation">
        <el-button
          type="primary"
          class="my-primary2"
          icon="el-icon-circle-plus-outline"
          @click="addFormFn"
        >新建
        </el-button>
      </el-row>
      <!-- 表单主体 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="(index) => (search.pageIndex - 1) * 10 + (index + 1)"
          label="序号"
          width="80"
        />
        <el-table-column prop="className" label="商品类型名称" />
        <el-table-column label="操作" width="150">
          <template v-slot="{ row }">
            <el-button
              type="text"
              class="el-button--primary-text"
              @click="modifyGoodsType(row)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              class="el-button--primary-text del-color"
              @click="delGoodsType(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单底部 -->
      <div class="pagination-container">
        <el-pagination
          layout="slot,prev,next"
          :current-page.sync="search.pageIndex"
          prev-text="上一页"
          next-text="下一页"
          background
          hide-on-single-page
          :total="totalCount"
          :page-size="search.pageSize"
          @current-change="getGoodsType"
        >
          <template order="-1">
            <span
              class="el-pagination__total"
            >共{{ totalCount }}条记录 第 {{ search.pageIndex }}/{{
              totalPage
            }}
              页</span>
          </template>
        </el-pagination>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog
      :title="`${currentId ? '修改' : '新增'}商品类型`"
      :visible.sync="addFormDialog"
      width="630px"
      @close="closeFn"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="140px"
      >
        <el-form-item label="商品类型名称：" prop="className">
          <el-input
            v-model="addForm.className"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="10"
          />
        </el-form-item>

        <div class="dialog-footer">
          <el-row type="flex" align="middle" justify="center">
            <el-col style="text-align: center">
              <el-button
                class="my-secondary"
                @click="addFormDialog = false"
              >取 消</el-button>
              <el-button
                type="primary"
                class="my-primary2"
                style="margin-left: 34px"
                @click="saveData"
              >确 定</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-dialog>
    <!-- 查看详情 -->
  </div>
</template>
<script>
import {
  getGoodsType,
  addGoodsType,
  modifyGoodsType,
  delGoodsType
} from '@/api/goods'

export default {
  name: 'GoodsTypeContainer',
  data() {
    return {
      search: {
        pageIndex: 1,
        pageSize: 10,
        className: ''
      },
      addFormDialog: false,
      detailDialog: false,
      addForm: {
        className: '',
        classId: ''
      },
      policyDetail: [],
      tableData: [],
      totalPage: 0,
      totalCount: 0,
      currentName: '',
      currentId: null,
      rules: {
        policyName: [{ required: true, message: '请输入', trigger: 'change' }],
        discount: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getGoodsType()
  },
  methods: {
    // 查看详情
    async showDetailDialog(row) {
      console.log(row)
      this.detailDialog = true
    },
    // 查找
    async getGoodsType() {
      const res = await getGoodsType({ ...this.search })
      console.log(res)
      this.tableData = res.currentPageRecords
      this.totalCount = +res.totalCount
      this.totalPage = +res.totalPage
    },
    // 新增
    async addFormFn() {
      this.addFormDialog = true
    },
    // 删除
    async delGoodsType(row) {
      await delGoodsType(row.classId)
      this.$message.success('删除策略成功')
      this.getGoodsType()
    },
    // 关闭弹窗(清空表单)
    closeFn() {
      this.currentId = null
      this.addForm = {
        className: ''
      }
      this.$refs.addForm.resetFields()
      this.getGoodsType()
      this.addFormDialog = false
    },
    // 修改
    async modifyGoodsType(row) {
      console.log(row)
      this.currentId = row.classId
      this.addForm.className = row.className
      this.addFormDialog = true
    },
    // 确认
    async saveData() {
      this.$refs.addForm.validate(async(pass) => {
        if (!pass) return
        this.currentId
          ? await modifyGoodsType({
            classId: this.currentId,
            className: this.addForm.className
          })
          : await addGoodsType({ ...this.addForm })
        this.$message.success(`${this.currentId ? '编辑' : '新增'}成功`)
        this.closeFn()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/common.scss";
.search .el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  font-weight: normal;
}

.taskDetail .el-form-item {
  margin-bottom: 0;
}
</style>
